$(function () {
    //常用jQuery对象
    var incomeManage_datagrid = $("#incomeManage_list");
    var incomeManage_dailog = $("#incomeManage_dialog");
    var incomeManage_form = $("#incomeManage_form");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            incomeManage_form.form("clear");
            incomeManage_dailog.dialog("open");
            incomeManage_dailog.dialog("setTitle", "新增");

        },

        //编辑按钮方法
        edit: function () {
            var row = incomeManage_datagrid.datagrid("getSelected");
            console.log(row);
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            //回显学员名称
            if (row.clientName) {
                row["clientName.name"] = row.clientName.name;
            }
            //回显班级名称

            if (row.className) {
                row["className.name"] = row.className.name;
            }

            //回显收款时间
            if (row.incomeTime) {
                row["incomeTime.id"] = row.incomeTime.id;
            }
            //回显收款金额
            if (row.incomeMoney) {
                row["incomeMoney.id"] = row.incomeMoney.id;
            }
            //回显还欠学费
            if (row.debtStudent) {
                row["debtStudent.ownTuition"] = row.debtStudent.ownTuition;
            }
            //回显收款类型
            if (row.incomeType) {
                row["incomeType.id"] = row.incomeType.id;
            }
            //回显收款人
            if (row.incomeMan) {
                row["incomeMan.id"] = row.incomeMan.id;
            }
            //回显单据号
            if (row.receipt) {
                row["receipt.id"] = row.receipt.id;
            }
            //回显营销人员
            if (row.salesman) {
                row["salesman.username"] = row.salesman.username;
            }
            //回显备注
            if (row.remark) {
                row["remark.id"] = row.remark.id;
            }
            incomeManage_dailog.dialog("open");
            incomeManage_dailog.dialog("setTitle", "编辑");
            incomeManage_dailog.form("load", row);
            console.log(this)
            },

        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            incomeManage_form.form("submit", {
                url: "/incomeManage/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            incomeManage_dailog.dialog("close");
                            incomeManage_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            incomeManage_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        //删除按钮方法
        btn_remove: function () {
            var row = incomeManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此数据吗?", function (r) {
                if (r) {
                    $.get("/incomeManage/delete.do", {id: row.id}, function (data) {
                        console.log(data);
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                incomeManage_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });
        },


        //审核按钮
        changeState: function () {
            var row = incomeManage_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示', "亲,您确定要审核吗？", function (r) {
                if (r) {
                    $.get("/incomeManage/changState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                incomeManage_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            $("#keyword").textbox("setValue", "");
            $("#classId").combobox("setValue", "");
            var beginDate = $("#beginDate").datebox("setValue", "");
            var endDate = $("#endDate").datebox("setValue", "");
            incomeManage_datagrid.datagrid("reload", {classId: "-1"});
        },

        //高级查询按钮
        searchs: function () {
            var keyword = $("#keyword").textbox("getValue");
            var classId = $("#classId").combobox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");
            incomeManage_datagrid.datagrid("load", {
                keyword: keyword,
                classId: classId,
                beginDate: beginDate,
                endDate: endDate
            });
        }

    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    incomeManage_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/incomeManage/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_edit").hide();
                $("#btn_del").hide();
                $("#btn_changeState").linkbutton({
                    text: "已审核"
                }).hide();
            } else {
                $("#btn_edit").show();
                $("#btn_del").show();
                $("#btn_changeState").linkbutton({
                    text: "审核"
                }).show();

            }
        },
        columns: [[
            {
                field: 'clientName', title: '学员名称', width: 100, formatter: function (clientName) {
                    console.log(clientName);
                    return clientName ? clientName.name : '';
                }
            },
            {
                field: 'className', title: '班级名称', width: 100, formatter: function (className) {
                    return className? className.name : '';
                }
            },
            // {
            //     field: 'id', title: '班级名称',hidden:true, width: 100, formatter: function (className) {
            //         return className? className.id : '';
            //     }
            // },
            {field: 'incomeTime', title: '收款时间', width: 100, align: "center"},
            {field: 'incomeMoney', title: '收款金额', width: 100, align: "center"},
            {
                field: 'debtStudent', title: '还欠学费', width: 100, formatter: function (debtStudent) {
                    return debtStudent ? debtStudent.ownTuition : 0 ;
                }
            },
            {
                field: 'incomeType', title: '收款类型', width: 100, formatter: function (incomeType) {
                    return incomeType ? incomeType.name : "<font color='red'>未选择</font>";
                }
            },
            {
                field: 'incomeMan', title: '收款人', width: 100, formatter: function (incomeMan) {
                    return incomeMan ? incomeMan.username : "<font color='red'>未选择</font>";
                }
            },
            {field: 'receipt', title: '单据号', width: 100, align: "center"},
            {
                field: 'salesman', title: '营销人员', width: 100, formatter: function (salesman) {
                    return salesman ? salesman.username : '';
                }
            },
            {field: 'remark', title: '备注', width: 100, align: "center"},
            {
                field: 'state', title: '审核状态', width: 100, formatter: function (state) {
                    return state ? '已审核' : "<font color='red'>未审核</font>";
                }
            },
            {
                field: 'auditor', title: '审核人', width: 100, formatter: function (auditor) {
                    return auditor ? auditor.username : auditor;
                }
            },
        ]],
        toolbar: "#tools"
    });

    //编辑添加弹出框设置
    incomeManage_dailog.dialog({
        width: 350,
        height: 500,
        closed: true,
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                incomeManage_dailog.dialog("close");
            }
        }]
    });

    //form表单设置
    incomeManage_dailog.form({
        fit: true
    });
    //下拉框点击事件
    $("#clientNames").combobox({
        onChange:function () {
            var clientId = $("#clientNames").combobox("getValue");
            console.log(clientId);

            $.get('/studentofficial/getOwnTuitionByClientId.do',{id:clientId},function (data) {

                console.log(data);
                $("#ownTuition").numberbox("setValue",data.ownTuition);
            $("#classname").textbox("setValue",data.classname.name);
            $("#salesman").textbox("setValue",data.saleman.username);

                    },'json')
                }
          })





});

